package com.lijian.shiro.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lijian.shiro.entity.User;
import com.lijian.shiro.mapper.UserMapper;
import com.lijian.shiro.service.UserService;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: LiJian
 * @CreateTime: 2024-12-26
 * Description:
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public User getUserInfoByName(String name) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("name",name);
        User user = userMapper.selectOne(wrapper);
        return user;
    }

    @Override
    public String addUser(User user) {
       try {
           Md5Hash md5Hash = new Md5Hash(user.getPwd(), "salt",3);
           user.setPwd(md5Hash.toHex());
           userMapper.insert(user);
       }catch (Exception e){
           e.printStackTrace();
           return "用户注册失败";
       }
        return "用户注册成功";
    }

    @Override
    public User findUserByPhone(String phone) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("phone",phone);
        User user = userMapper.selectOne(queryWrapper);
        return user;
    }

    @Override
    public List<String> getUserRoleInfo(String principal) {
        return userMapper.getUserRoleInfoMapper(principal);
    }

    @Override
    public List<String> getUserPermissionInfo(List<String> roles) {
        return userMapper.getUserPermissionInfoMapper(roles);
    }
}
